<?php
function getdb_cen( $cid, $sql, $page = 0, $pagesize = 20 )
{
    $rs = new RecordSet_CEN( $cid );
    $rs->db->chgCidConnect( $cid );
    $rs->open( $sql, $page, $pagesize );
    return $rs;
}

class DB_CEN extends DB
{

    public $handle = false;
    public $handle_master = false;
    public $server;
    public $user;
    public $pass;
    public $database;
    public $count = 0;
    public $result = 0;
    public $cid = 0;
    public $chg_c_flag = 0;

    public function DB_CEN( $cid = 0 )
    {
        global $CORE_CEN;
        $this->setCid( $cid );
        if ( !isset( $CORE_CEN['db_connection'] ) )
        {
            $this->connect();
        }
    }

    public function setCid( $cid = 0 )
    {
        global $g_cen_info;
        if ( isset( $g_cen_info[$cid] ) )
        {
            if ( $this->cid != $cid )
            {
                $this->chg_c_flag = 1;
            }
            $this->cid = $cid;
            return true;
        }
        exit( "error 300 - ".$cid );
    }

    public function chgCidConnect( $cid )
    {
        $this->setCid( $cid );
        $this->connect();
    }

    public function chkCidConnect( $cid )
    {
        global $g_cen_info;
        if ( isset( $g_cen_info[$cid] ) )
        {
            $dbinfo = $g_cen_info[$cid];
        }
        else
        {
            exit( "error 300 - ".$cid );
        }
        $sitename = $dbinfo['siteName'];
        $handle = mysql_connect( $dbinfo['dbser'], $dbinfo['dbuser'], $dbinfo['dbpw'] );
        $this->chg_c_flag = 1;
        if ( $handle == false )
        {
            return "error 300 - {$this->cid}连接 {$sitename} 资料库主机失败！";
        }
        if ( false == mysql_select_db( $dbinfo['dbbase'], $handle ) )
        {
            return "error 300 - {$this->cid}连接 {$sitename} 资料库失败！";
        }
        return 0;
    }

    public function connect( )
    {
        global $CORE_CEN;
        global $g_cen_info;
        global $g_db_sql_mode;
        if ( $this->chg_c_flag || !isset( $CORE_CEN['db_connection'] ) )
        {
            if ( isset( $g_cen_info ) && is_array( $g_cen_info ) )
            {
                $dbinfo = $g_cen_info[$this->cid];
                $this->server = $dbinfo['dbser'];
                $this->user = $dbinfo['dbuser'];
                $this->pass = $dbinfo['dbpw'];
                $this->database = $dbinfo['dbbase'];
                $handle = mysql_connect( $this->server, $this->user, $this->pass );
                mysql_query( $dbinfo['sql_mode'] );
                if ( $handle == false )
                {
                    exit( "error 300 - {$this->cid}连接 MASTER 资料库主机失败！".mysql_error( ) );
                }
                if ( false == mysql_select_db( $this->database, $handle ) )
                {
                    exit( "error 300 - {$this->cid}连接 MASTER 资料库失败！".mysql_error( ) );
                }
                $CORE_CEN['db_connection_master'] = $handle;
                $CORE_CEN['db_connection'] = $handle;
            }
            else
            {
                exit( "error 300 - 参数设置错误！" );
            }
            $this->chg_c_flag = 0;
        }
        $this->handle = $CORE_CEN['db_connection'];
        $this->handle_master = $CORE_CEN['db_connection_master'];
        return $this->handle;
    }

}

class RecordSet_CEN extends RecordSet
{

    public $result = false;
    public $eof = true;
    public $f = array();
    public $db;
    public $page = 0;
    public $pagesize = 20;
    public $currpos = 0;
    public $totalrecords = 0;
    public $totalpages = 0;
    public $cid = 0;

    public function RecordSet_CEN( $cid = 0 )
    {
        global $CORE_CEN;
        global $g_cen_info;
        if ( isset( $g_cen_info[$cid] ) )
        {
            $this->cid = $cid;
        }
        else
        {
            exit( "error 300 - R - ".$cid );
        }
        if ( !isset( $CORE_CEN['db'] ) )
        {
            $CORE_CEN['db'] = new DB_CEN( $cid );
        }
        $this->db = $CORE_CEN['db'];
    }

}

?>